package com.restaurant.application.indexes.bookingsetlist.mysql;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.nebutown.base.Unit;
import com.nebutown.description.data.SortSpec;
import com.nebutown.generation.basictype.UID;
import com.nebutown.supporthome.AbstractIndex;

import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

public class BookingSetListImpl extends AbstractIndex<BookingSetListCriteria, BookingSetListBean> {

    @Override
    public String getTableName() {
        return "" + getConnectionName() + ".BOOKINGSETLIST";
    }

    @Override
    public BookingSetListBean newBean() {
        return new BookingSetListBean();
    }

    @Override
    public BookingSetListCriteria newCriteria() {
        return new BookingSetListCriteria();
    }

    @Override
    public List<String> getFieldNames() {
        return Lists.newArrayList(
                "bookingSet",
                "id",
                "dates",
                "times",
                "service",
                "createTime",
                "modifyTime",
                "shop",
                "shopId",
                "shopName");
    }

    @Override
    public List<String> getDateFields() {
        return Lists.newArrayList(
                "createTime",
                "modifyTime");
    }

    @Override
    public List<String> getDoubleFields() {
        return Lists.newArrayList(
                "");
    }

    @Override
    public Map<String, String> getFieldLabels() {
        return ImmutableMap.<String, String>builder()
                .put("id", "设置Id")
                .put("dates", "日期段")
                .put("times", "时间段")
                .put("service", "服务")
                .put("createTime", "创建时间")
                .put("modifyTime", "修改时间")
                .put("shopId", "店铺Id")
                .put("shopName", "店铺名称")
                .build();
    }

    @Override
    public List<String> getContributors() {
        return Lists.newArrayList(
                "com.restaurant.application.bookingset.BookingSet",
                "com.restaurant.application.shop.Shop");
    }

    @Override
    public BookingSetListBean fakeBean() {
        BookingSetListBean bean = new BookingSetListBean();

        bean.setBookingSet(UID.recover("A -5675618739734789437 looooong string"));
        bean.setId("A 273952966564402865 looooong string");
        bean.setDates("A 7657992740155177507 looooong string");
        bean.setTimes("A 7577548810349383864 looooong string");
        bean.setService("A 2818511037063664893 looooong string");
        bean.setCreateTime(1995887439L);
        bean.setModifyTime(-84843827L);
        bean.setShop(UID.recover("A -1648452427054256586 looooong string"));
        bean.setShopId("A -3316397245789602057 looooong string");
        bean.setShopName("A -421141660066823730 looooong string");
        return bean;
    }

    @Override
    public String getCreateTableStatement() {
        StringBuilder buf = new StringBuilder();

        buf.append("CREATE TABLE IF NOT EXISTS " + getConnectionName() + ".BOOKINGSETLIST (\n");
        buf.append("    RECORD_ID BIGINT AUTO_INCREMENT,\n");
        buf.append("    BOOKINGSET CHAR(128) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,\n");
        buf.append("    ID VARCHAR(255),\n");
        buf.append("    DATES VARCHAR(255),\n");
        buf.append("    TIMES VARCHAR(255),\n");
        buf.append("    SERVICE VARCHAR(255),\n");
        buf.append("    CREATETIME BIGINT,\n");
        buf.append("    MODIFYTIME BIGINT,\n");
        buf.append("    SHOP CHAR(128) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,\n");
        buf.append("    SHOPID VARCHAR(255),\n");
        buf.append("    SHOPNAME VARCHAR(255),\n");
        buf.append("PRIMARY KEY (RECORD_ID)) CHARACTER SET  = utf8mb4 COLLATE = utf8mb4_bin");
        return buf.toString();
    }

    @Override
    public String getInsertDataStatement(BookingSetListBean data) {
        StringBuilder buf = new StringBuilder();

        buf.append("INSERT INTO " + getConnectionName() + ".BOOKINGSETLIST (\n    ");
        buf.append("    BOOKINGSET, ID, DATES, TIMES, SERVICE, CREATETIME, MODIFYTIME, SHOP, SHOPID, SHOPNAME");
        buf.append(") VALUES (");
        buf.append("    '" + cut(data.getBookingSet(), 128) + "', '" + cut(data.getId(), 255) + "', '" + cut(data.getDates(), 255) + "', '" + cut(data.getTimes(), 255) + "', '" + cut(data.getService(), 255) + "', " + data.getCreateTime() + ", " + data.getModifyTime() + ", '" + cut(data.getShop(), 128) + "', '" + cut(data.getShopId(), 255) + "', '" + cut(data.getShopName(), 255) + "'");
        buf.append(")");
        return buf.toString();
    }

    @Override
    public String getDeleteRowsStatement(Long rowId) {
        return "DELETE FROM " + getConnectionName() + ".BOOKINGSETLIST WHERE  RECORD_ID = " + rowId;
    }

    @Override
    public String getLocalUpdateStatement(BookingSetListBean data) {
        StringBuilder buf = new StringBuilder();
        StringBuilder sep = new StringBuilder();

        boolean hasData = false;

        buf.append("UPDATE " + getConnectionName() + ".BOOKINGSETLIST \n");
        buf.append("   SET ");
        if (data.getBookingSet() != null) {
            buf.append(sep.toString() + "\n      BOOKINGSET = '" + cut(data.getBookingSet(), 128) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getId() != null) {
            buf.append(sep.toString() + "\n      ID = '" + cut(data.getId(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getDates() != null) {
            buf.append(sep.toString() + "\n      DATES = '" + cut(data.getDates(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getTimes() != null) {
            buf.append(sep.toString() + "\n      TIMES = '" + cut(data.getTimes(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getService() != null) {
            buf.append(sep.toString() + "\n      SERVICE = '" + cut(data.getService(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getCreateTime() != null) {
            buf.append(sep.toString() + "\n      CREATETIME = " + data.getCreateTime() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getModifyTime() != null) {
            buf.append(sep.toString() + "\n      MODIFYTIME = " + data.getModifyTime() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getShop() != null) {
            buf.append(sep.toString() + "\n      SHOP = '" + cut(data.getShop(), 128) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getShopId() != null) {
            buf.append(sep.toString() + "\n      SHOPID = '" + cut(data.getShopId(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getShopName() != null) {
            buf.append(sep.toString() + "\n      SHOPNAME = '" + cut(data.getShopName(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (!hasData) return "";
        
        hasData = false;
        buf.append(" WHERE TRUE\n");
        if (data.getBookingSet() != null) {
            buf.append("    AND BOOKINGSET = '" + data.getBookingSet() + "'\n");
            hasData = true;
        }
        if (data.getShop() != null) {
            buf.append("    AND SHOP = '" + data.getShop() + "'\n");
            hasData = true;
        }
        if (!hasData) return "";
        
        return buf.toString();
    }

    @Override
    public String getUpdateByIdStatement(BookingSetListBean data) {
        StringBuilder buf = new StringBuilder();
        StringBuilder sep = new StringBuilder();

        boolean hasData = false;

        buf.append("UPDATE " + getConnectionName() + ".BOOKINGSETLIST \n");
        buf.append("   SET ");
        if (data.getBookingSet() != null) {
            buf.append(sep.toString() + "\n      BOOKINGSET = '" + cut(data.getBookingSet(), 128) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getId() != null) {
            buf.append(sep.toString() + "\n      ID = '" + cut(data.getId(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getDates() != null) {
            buf.append(sep.toString() + "\n      DATES = '" + cut(data.getDates(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getTimes() != null) {
            buf.append(sep.toString() + "\n      TIMES = '" + cut(data.getTimes(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getService() != null) {
            buf.append(sep.toString() + "\n      SERVICE = '" + cut(data.getService(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getCreateTime() != null) {
            buf.append(sep.toString() + "\n      CREATETIME = " + data.getCreateTime() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getModifyTime() != null) {
            buf.append(sep.toString() + "\n      MODIFYTIME = " + data.getModifyTime() + "");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getShop() != null) {
            buf.append(sep.toString() + "\n      SHOP = '" + cut(data.getShop(), 128) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getShopId() != null) {
            buf.append(sep.toString() + "\n      SHOPID = '" + cut(data.getShopId(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (data.getShopName() != null) {
            buf.append(sep.toString() + "\n      SHOPNAME = '" + cut(data.getShopName(), 255) + "'");
            hasData = true;
            sep.setLength(0);
            sep.append(",");
        }
        if (!hasData) return "";
        
        hasData = false;
        buf.append(" WHERE RECORD_ID = " + data.getRecordId());
        return buf.toString();
    }

    @Override
    public BookingSetListBean extractLine(ResultSet resultSet) throws Exception {
        BookingSetListBean result = new BookingSetListBean();

        result.setRecordId(resultSet.getLong("RECORD_ID"));
        result.setBookingSet(UID.recover(resultSet.getString("bookingSet")));
        result.setId(resultSet.getString("id"));
        result.setDates(resultSet.getString("dates"));
        result.setTimes(resultSet.getString("times"));
        result.setService(resultSet.getString("service"));
        result.setCreateTime(resultSet.getLong("createTime"));
        result.setModifyTime(resultSet.getLong("modifyTime"));
        result.setShop(UID.recover(resultSet.getString("shop")));
        result.setShopId(resultSet.getString("shopId"));
        result.setShopName(resultSet.getString("shopName"));
        return result;
    }

    @Override
    public String getWherePart(BookingSetListCriteria criteria, SortSpec sort) {
        StringBuilder buf = new StringBuilder(" TRUE ");

        criteria.setIdLike(searchString(criteria.getIdLike()));
        criteria.setDatesLike(searchString(criteria.getDatesLike()));
        criteria.setTimesLike(searchString(criteria.getTimesLike()));
        criteria.setServiceLike(searchString(criteria.getServiceLike()));
        criteria.setShopIdLike(searchString(criteria.getShopIdLike()));
        criteria.setShopNameLike(searchString(criteria.getShopNameLike()));

        if (criteria.getRecordId() != null) {
            buf.append("    AND RECORD_ID = " + criteria.getRecordId());
        }
        if (criteria.getBookingSetEquals() != null) {
            buf.append("    AND BOOKINGSET = '" + criteria.getBookingSetEquals() + "'");
        }
        if (criteria.getBookingSetNotEquals() != null) {
            buf.append("    AND BOOKINGSET != '" + criteria.getBookingSetNotEquals() + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getIdEquals())) {
            buf.append("    AND ID = '" + cut(criteria.getIdEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getIdNotEquals())) {
            buf.append("    AND ID != '" + cut(criteria.getIdNotEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getIdLike())) {
            buf.append("    AND ID LIKE '" + cut(criteria.getIdLike()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getDatesEquals())) {
            buf.append("    AND DATES = '" + cut(criteria.getDatesEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getDatesNotEquals())) {
            buf.append("    AND DATES != '" + cut(criteria.getDatesNotEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getDatesLike())) {
            buf.append("    AND DATES LIKE '" + cut(criteria.getDatesLike()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getTimesEquals())) {
            buf.append("    AND TIMES = '" + cut(criteria.getTimesEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getTimesNotEquals())) {
            buf.append("    AND TIMES != '" + cut(criteria.getTimesNotEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getTimesLike())) {
            buf.append("    AND TIMES LIKE '" + cut(criteria.getTimesLike()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getServiceEquals())) {
            buf.append("    AND SERVICE = '" + cut(criteria.getServiceEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getServiceNotEquals())) {
            buf.append("    AND SERVICE != '" + cut(criteria.getServiceNotEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getServiceLike())) {
            buf.append("    AND SERVICE LIKE '" + cut(criteria.getServiceLike()) + "'");
        }
        if (criteria.getCreateTimeUpper() != null) {
            buf.append("    AND CREATETIME <= " + criteria.getCreateTimeUpper());
        }
        if (criteria.getCreateTimeLower() != null) {
            buf.append("    AND CREATETIME >= " + criteria.getCreateTimeLower());
        }
        if (criteria.getCreateTimeEquals() != null) {
            buf.append("    AND CREATETIME = '" + criteria.getCreateTimeEquals() + "'");
        }
        if (criteria.getCreateTimeNotEquals() != null) {
            buf.append("    AND CREATETIME != '" + criteria.getCreateTimeNotEquals() + "'");
        }
        if (criteria.getModifyTimeUpper() != null) {
            buf.append("    AND MODIFYTIME <= " + criteria.getModifyTimeUpper());
        }
        if (criteria.getModifyTimeLower() != null) {
            buf.append("    AND MODIFYTIME >= " + criteria.getModifyTimeLower());
        }
        if (criteria.getModifyTimeEquals() != null) {
            buf.append("    AND MODIFYTIME = '" + criteria.getModifyTimeEquals() + "'");
        }
        if (criteria.getModifyTimeNotEquals() != null) {
            buf.append("    AND MODIFYTIME != '" + criteria.getModifyTimeNotEquals() + "'");
        }
        if (criteria.getShopEquals() != null) {
            buf.append("    AND SHOP = '" + criteria.getShopEquals() + "'");
        }
        if (criteria.getShopNotEquals() != null) {
            buf.append("    AND SHOP != '" + criteria.getShopNotEquals() + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getShopIdEquals())) {
            buf.append("    AND SHOPID = '" + cut(criteria.getShopIdEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getShopIdNotEquals())) {
            buf.append("    AND SHOPID != '" + cut(criteria.getShopIdNotEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getShopIdLike())) {
            buf.append("    AND SHOPID LIKE '" + cut(criteria.getShopIdLike()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getShopNameEquals())) {
            buf.append("    AND SHOPNAME = '" + cut(criteria.getShopNameEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getShopNameNotEquals())) {
            buf.append("    AND SHOPNAME != '" + cut(criteria.getShopNameNotEquals()) + "'");
        }
        if (!Strings.isNullOrEmpty(criteria.getShopNameLike())) {
            buf.append("    AND SHOPNAME LIKE '" + cut(criteria.getShopNameLike()) + "'");
        }
        if (sort != null && sort.getColumns().size() > 0) {
            buf.append(" ORDER BY ");
            sort.getColumns().forEach(c -> buf.append(c.getColumnName().toUpperCase())
                    .append(" ").append((c.getAscending() != null && c.getAscending()) ? "ASC" : "DESC")
                    .append(", "));
            buf.append("RECORD_ID ASC");
        }
        return buf.toString();
    }

    @Override
    public <T extends Unit> String getUpdateStatement(T source) {
        BookingSetListBean data = null;

        if (source instanceof com.restaurant.application.indexes.bookingsetlist.mysql.BookingSetListFrombookingSetIF) {
            data = new BookingSetListBean();
            ((com.restaurant.application.indexes.bookingsetlist.mysql.BookingSetListFrombookingSetIF) source).getBookingSetDataForBookingSetList(data);
        }
        if (source instanceof com.restaurant.application.indexes.bookingsetlist.mysql.BookingSetListFromshopIF) {
            data = new BookingSetListBean();
            ((com.restaurant.application.indexes.bookingsetlist.mysql.BookingSetListFromshopIF) source).getShopDataForBookingSetList(data);
        }

        return data == null ? null : getLocalUpdateStatement(data);
    }
}
